<?php
namespace Play\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;

//
class IndexController extends Controller
{

    /**
     * 初始化链
     *
     * @descript 如果未登录跳转至登录页面
     */
	function _initialize(){

        // if (!session('?user')) {
        //     $this->redirect('/Home/Login/index');
        // }

    }
	
    function test1(){
    	$this->display();
    }
	function test2(){
		$this->display();
	}
	function test3(){
		$this->display();
	}
	function test4(){
		$this->display();
	}
	function play(){
		$title = I('title');
		$mp4 = I('mp4');
		echo $title;die;
		$this->assign('title',$title);
		$this->assign('mp4',$mp4);
		$this->display();
	}
	
	
    /**
     * 首页的数据
     */
	public function index(){
		$this->display();
	}
    /**
     * 一个包含网页的frame，提供底部tab
     */
    public function zhuye(){
        $class=M('classify');
        $list=$class->field('id,name')->where('pid>6')->order('id desc')->select();
        $lecture=M('lecture');
        $list_act=$lecture->field("id,title,peoplecount,image")->order('id desc')->limit(3)->select();
        $this->assign("list", $list);
        $this->assign("list_act", $list_act);
        $this->display();
    }

    /**
     * 我的方案首页
     */
    public function fangan(){
        $user=session('user');
        $class=M('my_treatment');
        $list=$class->table("treatment as a,my_treatment as b")->field('a.id,a.name,a.descs')->where("a.id=b.fangan_id and b.user_id=(select U_ID from yh_system_users where U_LOGINNAME='".$user."')")->order('a.id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 活动讲座首页
     */
    public function act(){
        $lecture=M('lecture');
        $list=$lecture->field("id,title,peoplecount,image")->order('id desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 会员中心首页
     */
    public function user(){
        $userInfo=M('yh_system_users_info');
        $user=session('user');
        $info=$userInfo->table("yh_system_users_info as a,yh_system_users as b")->where("a.U_ID=b.U_ID and b.U_LOGINNAME='".$user."'")->find();
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 分类页面
     */
    public function classify($id = 1)
    {
        $class=M('classify');
        $list=$class->field('id,name')->where('pid='.$id)->order('id desc')->select();
        foreach ($list as $k=>$v){
            $chlid_list=$class->field('id,name,details')->where('pid='.$v['id'])->order('id desc')->select();
            $list[$k]['child']=$chlid_list;
        }
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 疾病详情
     */
    public function sick($id = 1)
    {
        $user=session('user');
        // $disease=M('disease');
        $disease=M('classify');
        $treatment=M('treatment');
        $myTreatment=M('my_treatment');
        $info=$disease->field('id,name,details')->where("id=".$id)->find();
        $info['treatment']=$treatment->field('id,name,descs,details,prise')->where('diseaseid='.$info['id'])->order('id desc')->select();
        foreach ($info['treatment'] as $k=>$v){
            $info['treatment'][$k]['cnt']=$myTreatment->where('fangan_id='.$v['id'])->count('id');
            // $pay=$myTreatment->field('id')->where('user_id=(select U_ID from yh_system_users where U_LOGINNAME='.$user.')')->find();
            // $info['treatment'][$k]['ispay']=(empty($pay)?0:1);
            $info['treatment'][$k]['no']=$k+1;

        }
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 活动详情
     */
    public function actdetail($id = 0)
    {
        $lecture=M('lecture');
        $info=$lecture->where("id=".$id)->find();
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 报名页面
     */
    public function baoming($id=0)
    {
        $lecture=M('lecture');
        $info=$lecture->where("id=".$id)->find();
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 报名处理
     */
    public function dobaoming()
    {
        // dump($_POST);die;
        $users=M('yh_system_users');
        $userInfo=M('yh_system_users_info');
        $user=session('user');
        $data=array();
        $da['lecture_id']=$_POST['id'];
        $da['time']=time();
        $data['U_ID']=$da['user_id']=$users->field("U_ID")->where("U_LOGINNAME='".$user."'")->find();
        $data['U_SEX']=$_POST['sex'];
        $data['U_PHONENUM']=$_POST['phone'];
        $data['U_IDNAME']=$_POST['name'];
        $data['U_COMPANY']=$_POST['company'];
        $status1=$userInfo->save($da);
        $status2=$userInfo->save($data);
        if($status1&&$status2){
            $info['status'] = 1;
            $info['info'] = '报名成功';
            $this->redirect('resultbaoming',
                            array(
                                'id' => I('id'),
                                'status' => 1
                            ));
        }else{
            $info['status'] = -1;
            $info['info'] = '报名失败';
            $this->redirect('resultbaoming',
                            array(
                                'id' => I('id'),
                                'status' => -1
                            ));
        }
    }

    /**
     * 报名成功
     */
    public function resultbaoming($id=0,$status = 1)
    {
        $lecture=M('lecture');
        $info=$lecture->where("id=".$id)->find();
        $this->assign("info", $info);
        $this->display();
    }
    
    /**
     * 我的活动
     */
    public function myact()
    {
        $user=session('user');
        $join_lecture=M('join_lecture');
        $list=$join_lecture->table("lecture as a,join_lecture as b")
             ->field('a.*')
             ->where("a.id=b.lecture_id and b.user_id=(select U_ID from yh_system_users where U_LOGINNAME='".$user."')")
             ->order('a.id desc')
             ->select();
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 积分管理
     */
    public function credit()
    {
        $user=session('user');
        $userInfo=M('yh_system_users_info');
        $info=$userInfo->table("yh_system_users_info as a,yh_system_users as b")->field("b.U_LOGINNAME,a.U_INTEGRAL")->where("a.U_ID=b.U_ID and b.U_LOGINNAME='".$user."'")->find();
        $this->assign("info", $info);
        $this->display();
    }

    /**
     *  关于我们
     */
    public function about()
    {
        $this->display();
    }
    
    /**
     * 支付界面
     */
    public function pay($id=0,$no=0){
        $treatment=M('treatment');
        $disease=M('disease');
        $info=$treatment->field('id,diseaseid,prise')->find();
        $info['dis_name']=$disease->field('name')->where('id='.$info['diseaseid'])->find()['name'];
        $info['no']=$no;
        $this->assign("info", $info);
        $this->display();
    }
    /**
     * 积分兑换
     */
    public function exchange($id=1,$no=0){
        $treatment=M('treatment');
        $disease=M('disease');
        $userInfo=M('yh_system_users_info');
        $user=session('user');
        $info=$treatment->field('id,diseaseid,prise')->find();
        $info['dis_name']=$disease->field('name')->where('id='.$info['diseaseid'])->find()['name'];
        $info['need_pay']=$info['prise']*10;
        $info['no']=$no;
        $info['integral']=$userInfo->field('U_INTEGRAL')->where('U_PHONENUM='.$user)->find()['u_integral'];
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 积分成功
     */
    public function resexchange($id=1){
        $treatment=M('treatment');
        $disease=M('disease');
        $myTreatment=M('my_treatment');
        $userInfo=M('yh_system_users_info');
        $users=M('yh_system_users');
        $user=session('user');
        $info=$treatment->field('id,diseaseid,prise')->find();
        $info['dis_name']=$disease->field('name')->where('id='.$info['diseaseid'])->find()['name'];
        $need_pay=$info['prise']*10;
        $integral=$userInfo->field('U_INTEGRAL')->where('U_PHONENUM='.$user)->find()['u_integral'];
        $status1=$userInfo->where('U_PHONENUM='.$user)->save(array('U_INTEGRAL'=>($integral-$need_pay)));
        $status2=$myTreatment->save(array('user_id'=>($users->field('U_ID')->where('U_LOGINNAME='.$user)->find()['u_id']),
                                          'fangan_id'=>$id
                                            ));
        if($status1&&$status2){
            $data['status'] = 1;
            $data['info'] = '发送失败';
            $this->ajaxReturn($data,'JSON');
        }else{
            $data['status'] = -1;
            $data['info'] = '兑换失败';
            $this->ajaxReturn($data,'JSON');
        }
    }

    public function checkShowMoreText($id = 0){
        $treatment=M('treatment');
        $t=$treatment->field('id,name,descs,details,prise,ispay')
                          ->where('id='.$id)
                          ->find();
        $info['status'] = $t['ispay'];
        $info['data'] = $t['details'];
        $info['prise'] = $t['prise'];
        $this->ajaxReturn($info);
    }

    public function jifenzhifu(){
        
    }

}